package gang.org.springframework.framework.web.servlet;

import gang.org.springframework.framework.context.GangApplicationContext;
import gang.org.springframework.framework.context.GangApplicationContextAware;
import gang.org.springframework.framework.context.GangConfigurableWebApplicationContext;
import gang.org.springframework.framework.web.context.GangWebApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：gang.chen
 * @Date：2023/10/13 9:39
 */
public abstract class GangFrameworkServlet extends GangHttpServletBean implements GangApplicationContextAware {

    public volatile boolean refreshEventReceived;

    public final Object onRefreshMonitor = new Object();
    public GangWebApplicationContext webApplicationContext;

    public void onRefresh(GangApplicationContext context){

        //For subclasses: do nothing by default.
    }

    /**
     * 当该对被实例化时，触发Aware超级接口，被初始化WebApplicationContext
     * */
    @Override
    public void setApplicationContext(GangApplicationContext applicationContext) {
        if (applicationContext instanceof GangWebApplicationContext) {
            this.webApplicationContext = (GangWebApplicationContext) applicationContext;
        }

    }

    @Override
    public void initServletBean() {

        //log TODO

        //long startTime = System.currentTimeMillis(); TODO

        try{

            this.webApplicationContext = initWebApplicationContext();

            //initFrameworkServlet();

        }catch (Exception e){
            //TODO
        }

    }

    private GangWebApplicationContext initWebApplicationContext() {

        //GangWebApplicationContext rootContext = GangWebApplicationContextUtils.getWebApplicationContext(getServletContext()); TODO

        GangWebApplicationContext wac = null;

        if(this.webApplicationContext != null){

            wac = this.webApplicationContext;

            if (wac instanceof GangConfigurableWebApplicationContext){

                //TODO
            }
        }

        if(wac == null){
            //TODO
        }

        if (wac == null) {
            //TODO
        }

        if (!this.refreshEventReceived) {

            synchronized (this.onRefreshMonitor) {

                onRefresh(wac);
            }

        }

        return wac;
    }

    public abstract void doService(HttpServletRequest request, HttpServletResponse response) throws Exception;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //TODO
        super.service(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //TODO
        processRequest(req, resp);
    }

    private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
        //TODO
        try {
            doService(req, resp);
        } catch (Exception e) {
            //TODO
        }
    }
}
